<template>
  <div>
    <div class="search">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="开课学期">
          <el-select v-model="searchForm.term" placeholder="请选择开课学期">
            <el-option label="全部" value="全部"></el-option>
            <el-option label="2024-2025-1" value="2024-2025-1"></el-option>
            <el-option label="2023-2024-2" value="2023-2024-2"></el-option>
            <el-option label="2023-2024-1" value="2023-2024-1"></el-option>
            <el-option label="2022-2023-2" value="2022-2023-2"></el-option>
            <el-option label="2022-2023-1" value="2022-2023-1"></el-option>
            <el-option label="2021-2022-2" value="2021-2022-2"></el-option>
            <el-option label="2021-2022-1" value="2021-2022-1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程名称">
          <el-input v-model="searchForm.cname"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="score">
      <el-table
      :data="scoreData"
      border
      style="width: 100%"
      max-height="550px">
        <el-table-column
          prop="term"
          label="开课学期"
          width="188"
          header-align="center"
          align="center">
        </el-table-column>
        <el-table-column
          prop="cid"
          label="课程编号"
          width="188"
          header-align="center"
          align="center">
        </el-table-column>
        <el-table-column
          prop="cname"
          label="课程名称"
          width="290"
          header-align="center"
          align="center">
        </el-table-column>
        <el-table-column
          prop="credits"
          label="课程学分"
          width="145"
          header-align="center"
          align="center">
        </el-table-column>
        <el-table-column
          prop="teachid"
          label="教学班"
          width="250"
          header-align="center"
          align="center">
        </el-table-column>
        <el-table-column
          prop="grade"
          label="成绩"
          width="145"
          header-align="center"
          align="center">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { getAllScore } from '@/api/score'
import { getScore } from '@/api/score'
export default {
  data(){
    return{
      scoreData:[],
      searchForm:{
        term:'全部',
        cname:''
      }
    }  
  },
  methods:{
    onSubmit(){
      if(this.searchForm.term==='全部'){
        if(!this.searchForm.cname){
          this.$nextTick(()=>{
            getAllScore().then(res=>{
              if(res.data.status===0){
                this.scoreData=res.data.data
                this.$message({
                  message: '查询成功！',
                  type: 'success'
                })
              }
            })
          })
        }else{
          getScore({cname:this.searchForm.cname}).then(res=>{
            if(res.data.status===0){
              this.scoreData=res.data.data
              this.$message({
                message: '查询成功！',
                type: 'success'
              })
            }else{
              this.$message({
                message: res.data.message,
                type: 'error'
              })
            }
          })
        }
      }else{
        if(!this.searchForm.cname){
          console.log(this.searchForm.term);
          getScore({term:this.searchForm.term}).then(res=>{
            if(res.data.status===0){
              this.scoreData=res.data.data
              this.$message({
                message: '查询成功！',
                type: 'success'
              })
            }else{
              this.$message({
                message:res.data.message,
                type: 'error'
              })
            }
          })
        }else{
          getScore({cname:this.searchForm.cname,term:this.searchForm.term}).then(res=>{
            if(res.data.status===0){
              this.scoreData=res.data.data
              this.$message({
                message: '查询成功！',
                type: 'success'
              })
            }else{
              this.$message({
                message: res.data.message,
                type: 'error'
              })
            }
          })
        }
      }
    }
  },
  mounted(){
    this.$nextTick(()=>{
      getAllScore().then(res=>{
        if(res.data.status===0){
          this.scoreData=res.data.data
        }
      })
    })
  }
}
</script>

<style>

</style>